#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include "Socket.hpp"
#include "Listener.hpp"
#include "Handlers.hpp"


static const class Addr localaddr("0.0.0.0",8889);
int main()
{
    // 守护进程
    /*pid_t pid = fork();
    if(pid == 0) // 子进程
    {
        signal(SIGCHLD,SIG_IGN);
        signal(SIGPIPE,SIG_IGN);
        if (chdir("/") == -1) 
        {
            perror("chdir");
            return 1;
        }
        int fd = open("/dev/null",O_WRONLY);
        dup2(fd,0);
        dup2(fd,1);
        dup2(fd,2);

    } 
    else // 父进程
    {
        exit(0);
    }*/
    // 设置套接字
    class TCP_Socket socket(localaddr);
    //  
    class Listener listener(socket.GetSocket(),localaddr);
    //
    class Reactor reactor;
    //
    class Handlers handlers;
    reactor.SetOnConnect(std::bind(&Handlers::Acceptor,&handlers,std::placeholders::_1));
    //
    reactor.AddConnect(listener.Sockfd(),localaddr,LISTENSOCKFD);
    //
    reactor.SetNormalHandlers(std::bind(&Handlers::NormalSockfdRead,&handlers,std::placeholders::_1),
                              std::bind(&Handlers::NormalSockfdWrite,&handlers,std::placeholders::_1),
                              std::bind(&Handlers::Expect,&handlers,std::placeholders::_1));
    reactor.Dispatcher();
    return 0;
}
